﻿using QFramework;
using UnityEngine;

namespace HKTools
{
    public interface IHK_BasePanelLifeCircle
    {
        void Init();
        void Open(string message);
        void Close();
        void Pause();
        void Resume();
    }

    [RequireComponent(typeof(CanvasGroup))]
    public abstract class HK_BasePanel : HKUIFramework_BaseController, IHK_BasePanelLifeCircle
    {
        [HideInInspector] public HK_PanelType PanelType;
        [HideInInspector] public PanelName PanelName;
        [HideInInspector] public int Order;

        CanvasGroup cg;
        protected CanvasGroup CG
        {
            get
            {
                if (cg == null)
                    cg = GetComponent<CanvasGroup>();

                return cg;
            }
        }

        IHK_UISystem uiSystem;
        protected IHK_UISystem UISystem
        {
            get
            {
                if (uiSystem == null)
                    uiSystem = this.GetSystem<IHK_UISystem>();

                return uiSystem;
            }
        }

        HK_BasePanelAnim panelAnim;
        bool isHavePanelAnim;

        /// <summary>
        /// 显示面板时，接收的参数
        /// </summary>
        protected BindableProperty<string> OpenMessage = new BindableProperty<string>();

        /// <summary>
        /// 初始化界面
        /// </summary>
        public abstract void OnInit();

        /// <summary>
        /// 显示界面
        /// </summary>
        public abstract void OnOpen();

        /// <summary>
        /// 退出界面
        /// </summary>
        public abstract void OnClose();

        /// <summary>
        /// 暂停界面
        /// </summary>
        public abstract void OnPause();

        /// <summary>
        /// 恢复界面
        /// </summary>
        public abstract void OnResume();

        public void Init()
        {
            OnInit();

            isHavePanelAnim = TryGetComponent<HK_BasePanelAnim>(out panelAnim);

            if (isHavePanelAnim)
                panelAnim.OnInitAnim();

            HKDebug.Log("Init 面板：" + this.name, LogColor.Blue);

            OpenMessage.Register(value =>
            {
                HKDebug.Log("面板 " + PanelName.value.ToString() + " 开启消息：" + value, LogColor.Cyan);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        public void Open(string message)
        {
            if (message != null)
                OpenMessage.Value = message;

            OnOpen();

            if (isHavePanelAnim)
                panelAnim.OnOpenAnim();

            HKDebug.Log("Open 面板：" + this.name, LogColor.Green);
        }

        public void Close()
        {
            OnClose();

            if (isHavePanelAnim)
                panelAnim.OnCloseAnim();

            HKDebug.Log("Close 面板：" + this.name, LogColor.Red);
        }

        public void Pause()
        {
            OnPause();

            if (isHavePanelAnim)
                panelAnim.OnPauseAnim();

            HKDebug.Log("Pause 面板：" + this.name, LogColor.Yellow);
        }

        public void Resume()
        {
            OnResume();

            if (isHavePanelAnim)
                panelAnim.OnResumeAnim();

            HKDebug.Log("Resume 面板：" + this.name, LogColor.Green);
        }
    }
}